package com.hzit.web.controller;


import com.hzit.proxy.base.ImageQRCode;
import com.hzit.proxy.message.MsgType;
import com.hzit.proxy.message.SendMessageProxy;
import com.hzit.proxy.user.UserInfoProxy;
import com.hzit.proxy.user.UserListProxy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@Controller
public class MainController {

    @RequestMapping("/main")
    public String main(Model model){

        String imageUrl = ImageQRCode.getImageUrl();
        UserListProxy userListProxy = new UserListProxy();
        List<String> userList = userListProxy.getUserList(null);
        UserInfoProxy userInfoProxy = new UserInfoProxy();
        List<Map<String, Object>> userInfo = userInfoProxy.getUserInfo();
        model.addAttribute("imageUrl",imageUrl);
        model.addAttribute("userList",userList);
        model.addAttribute("userInfo",userInfo);
        return "main";
    }
    @PostMapping("/send/msg")
    @ResponseBody
    public int sendMsg(@RequestParam(value = "openid",required = true) String openid,
                       @RequestParam(value="msgtype",required = true,defaultValue = "text") String msgtype,
                       @RequestParam(value = "content", required = false) String content,
                       @RequestParam(value = "title", required = false) String title,
                       @RequestParam(value = "desc", required = false) String desc,
                       @RequestParam(value = "url", required = false) String url,
                       @RequestParam(value = "picurl", required = false) String picurl
    ){
        SendMessageProxy sendMessageProxy = new SendMessageProxy();
        if(MsgType.TEXT.getMsgtype().equals(msgtype)){
            String ok = sendMessageProxy.sendTextMsg(openid, content);
            if("OK".equals(ok)){
                return 1;
            }
        }else if(MsgType.NEWS.getMsgtype().equals(msgtype)){
            String ok = sendMessageProxy.sendNewsMsg(openid, title, desc, url, picurl);
            if("OK".equals(ok)){
                return 1;
            }
        }else{
            System.out.println("暂时只支持文本和外链图文消息");
        }
        return 0;
    }
}
